import Vue from 'vue'
import VueRouter from 'vue-router'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location){
  return originalPush.call(this,location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Manager',
    component: () => import('../views/Manager.vue'),
    redirect:'/home',
    children: [
      { path: 'home', name: 'Home', meta: {name:'系统首页'},component: ()=> import('../views/Manager/Home')},
      { path: 'user', name: 'User' , meta: {name:'用户信息'},component: ()=> import('../views/Manager/User')},
      {path: '403', name: 'Auth',  meta: {name:'无权限'},component: () => import('../views/Manager/Auth')},
      {path: 'person', name: 'Person',  meta: {name:'个人信息'},component: () => import('../views/Manager/Person')},
      {path: 'password', name: 'Password',  meta: {name:'修改密码'},component: () => import('../views/Manager/Password')},
      {path: 'notice', name: 'Notice',  meta: {name:'系统公告'},component: () => import('../views/Manager/Notice')},
      {path: 'equipment', name: 'Equipment',  meta: {name:'设备管理'},component: () => import('../views/Manager/Equipment')},
      {path: 'repair', name: 'Repair', meta: {name:'设备报修'},component: () => import('../views/Manager/Repair.vue')},
      {path: 'repaircontrol', name: 'RepairControl', meta:{name:'报修管理'},component: () => import('../views/Manager/RepairControl')},
      {path: 'worker', name: 'Worker', meta:{name:'维修工管理'},component: () => import('../views/Manager/Worker')},
      {path: 'complain', name: 'Complain', meta:{name:'用户投诉'},component: () => import('../views/Manager/Complain')},
      {path: 'complainControl', name: 'ComplainControl', meta:{name:'投诉处理'},component: () => import('../views/Manager/ComplainControl')},
      {path: 'logs', name: 'Logs', meta: {name:'系统日志'},component: () => import('../views/Manager/Logs')},
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },

  {
    path: '*', name: '404', component: () => import('../views/404.vue')
  }



]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) =>{
  let adminPath = ['/user']
  let adminPath1 = ['/repaircontrol']
  let adminPath2 = ['/notice']
  let adminPath3 = ['/worker']
  let adminPath4 = ['/equipment']
  let user =JSON.parse(localStorage.getItem('honey-user') || '{}')
  if(user.role !=='管理员' && (adminPath.includes(to.path) || adminPath1.includes(to.path) || adminPath2.includes(to.path)
  || adminPath3.includes(to.path) || adminPath4.includes(to.path) )){
  next('/403')
  }
  next()
})

export default router
